import {get,post} from '../../http'
import { message } from 'antd';

const CLASSLIST = 'CLASSLIST';
const CLASSMENU = 'CLASSMENU'
const  ADDCLASS = 'ADDCLASS'
const ROUTULIST =  'ROUTULIST'
const DELECCLASS = 'DELECCLASS'
const BINDING = 'BINDING'
const DELECCLASS1 = 'DELECCLASS1'


const initState={
    ylClassificationDtoList:{},
    shipInfoDtoList:{},
    shipsList:{},
    delectRes:false,
    isADD:false,
    isState:false,
    delectClass:false,
    delectClass1:false,
    addBind:false
}

// reducer1
export function classList(state=initState, action){
	switch(action.type){
		case CLASSLIST:
			return Object.assign({},state,{shipInfoDtoList:action.data,isADD:false,delectClass1:false,isState:action.res,delectClass:false,addBind:false})
		case CLASSMENU:
            return Object.assign({},state,{ylClassificationDtoList:action.data,isADD:false,delectClass:false,delectClass1:false})
        case ADDCLASS:
            return Object.assign({},state,{isADD:action.data})
        case ROUTULIST:
            return Object.assign({},state,{shipsList:action.data,delectClass1:false,delectClass:false,addBind:false})
        case DELECCLASS:
            return Object.assign({},state,{delectClass:action.res}) 
        case DELECCLASS1:
            return Object.assign({},state,{delectClass1:action.res}) 
        case BINDING:
			return Object.assign({},state,{addBind:action.res}) 
		default:
			return state
	}
} 

function bindingSuccess(res){
	return {type:BINDING,res:res}
}
function delectSuccess(res){
    if(res){
		message.success("删除运力成功")
	}else{
		message.warning("删除运力失败")
	}
	return {type:DELECCLASS,res:res}
}
function delectSuccess1(res){
    if(res){
		message.success("删除运力成功")
	}else{
		message.warning("删除运力失败")
	}
	return {type:DELECCLASS1,res:res}
}
function classSuccess(data,res){
	return {type:CLASSLIST,data:data,res:res}
}
function classMenuSuccess(data){
	return {type:CLASSMENU,data:data}
}
function addSuccess(data){
	return {type:ADDCLASS,data:data}
}
function routuSuccess(data){
	return {type:ROUTULIST,data:data}
}
export function bindingOne(shipid,classificationId){
	let data = {shipid:shipid,classificationId:classificationId}
	return dispatch=>{
		post('/ylships/insertIYLShipsQuery.do',data)
			.then(res=>{
					dispatch(bindingSuccess(res.SUCCESS));
			})		
	}
	
}

export function deleclassTwo(classificationId){
	let data = {classificationId:classificationId}
	return dispatch=>{
		post('/ylclassifcation/deleteYLClassification.do',data)
			.then(res=>{
					dispatch(delectSuccess1(res.SUCCESS));
			})		
	}
	
}

export function deleclassOne(shipid,classificationId){
	let data = {shipid:shipid,classificationId:classificationId}
	return dispatch=>{
		post('/ylships/deleteIYLShips.do',data)
			.then(res=>{
					dispatch(delectSuccess(res.SUCCESS));
			})		
	}
	
}


export function routuOne(classificationId,page,limit,shopName){
	let data = {classificationId:classificationId,page:page,limit:limit,shopName:shopName}
	return dispatch=>{
		post('/ylclassifcation/selectNotClassificationShips.do',data)
			.then(res=>{
					dispatch(routuSuccess(res.shipsList));
			})		
	}	
}

export function caddOne(classificationName){
	let data = {classificationName:classificationName}
	return dispatch=>{
		post('/ylclassifcation/insertYLClassificationQuery.do',data)
			.then(res=>{
					dispatch(addSuccess(res.SUCCESS));
			})		
	}
	
}
export function classOne(page,limit,shipName,classificationId){
	let data = {page:page,limit:limit,shipName:shipName,classificationId:classificationId}
	return dispatch=>{
		post('/ylclassifcation/selectShipsByYLClassification.do',data)
			.then(res=>{
					dispatch(classSuccess(res.shipInfoDtoList,res.SUCCESS));
			})		
	}
	
}

export function ClassMenu(){
	return dispatch=>{
		post('/ylclassifcation/selectYLClassification.do',{})
			.then(res=>{
				dispatch(classMenuSuccess(res.ylClassificationDtoList));
					
			})		
	}
	
}


